
local save = {}
ac.game:event '游戏-初始化'(function(trg)
    trg:remove()
    for u in ac.selector('unit')
        : range(ac.point(0,0),20000)
        : can_god()
        : loop()
    do
        local id = u.owner.id
        local pt = u:get_point()
        local jd = u:get_facing()
        if save[id]==nil then
            save[id] = {}
        end
        save[id][#save[id] + 1] = {
            name = u.name,
            _id = FourCC(u._id),
            point = pt,
            angle = jd,
        }
        u:remove()
    end
end)


local rect1 = ac.rect.j_rect('第一幕')
local point_map = {
    ['出生点'] = 'point1',
    ['物品点'] = 'item_point',
    ['刷怪点'] = 'enemy_point',
}
local hide_shop = {
    ['闹鬼金矿'] = true,
    ['杀敌兑换'] = true,
    ['装备抽取'] = true,
}
local enemy_unit = {
    ['塔1'] = true,
    ['塔2'] = true,
    ['迷宫'] = true,
}

ac.game:event '玩家-加载预设单位'(function(_,player)
    local id = player.id
    for _,da in ipairs(save[id] or {}) do
        local pt = da.point
        local jd = da.angle
        local id = da._id
        local name = da.name
        local flag = rect1:has(da.point)
        local key1 = point_map[name]
        --记录点
        if key1 and flag then
            flag = false
            player[key1] = pt
        end

        if flag then
            local u = player:create_unit(id,pt,jd)
            u.owner_player = player
            if name=='基地' then
                ac.base_point = pt
                u:add_skill('基地设置')
            end
            if hide_shop[name] then
                u:add_buff '隐藏商店'{}
            end
            
            --怪物
            if enemy_unit[name] and player.enemy_player then
                u.owner_player = player
                u:set_owner(player.enemy_player)
                u:add_type('防御塔')
                u:add_buff '动态测试数据'{}
            end
        end
    end
end)


ac.game:event '游戏-设置难度结束'(function(_,hard)
    ac.init_game = true

    ac.each_player(function(player)
        local enemy = ac.player[player.id + 8]
        enemy:set_name(('%s的敌人'):format(player.name))
        player.enemy_player = enemy
        player:notify('玩家-注册数据',player)
    end)
    
    for a=1,16 do
        local player = ac.player[a]
        player:notify('玩家-加载预设单位',player)
    end
    ac.game:notify('游戏-注册数据完成')
end)


ac.buff('隐藏商店'){
    on_add = function(self)
        local u = self.owner
        local player = u.owner
        local p = u:get_point()
        u:add_restriction '隐藏'
        self:gc(player:event '玩家-显示商店'(function(trg,player,name,time)
            if name==u.name then
                self:remove()
                local time = time or 0
                player:set_camera(p,time)
                ac.wait(time*1000,function()
                    u:remove_restriction '隐藏'
                    u:notify('商店-显示',u)
                end)
            end
        end))
    end
}